<?php
$section = 'product_detail';
$titre = _LB_PRODUCT_MANAGEMENT;
$smarty->assign("titre", $titre);

//get object ID
$id = Request::getIntNumber('id');
$id_product = Request::getIntNumber('id_product');
$act = Request::getIntNumber('act');

// set backlink is product/list
$backlink = Session::getBackLink("product", Utils::getHomeLink());

$product_download = new Product_Download();	
if ($id_product) {
	$product_download->ID_PRODUCT = $id_product;
	$product = new Product();
	if (!$product->get($id_product)) {
		redirect($backlink);
	} else {
		// set backlink is product/viewDetail
		$backlink = Session::getBackLink($section, $backlink);
	}
}

if ($id) {
	if (!$product_download->get($id) || ($id_product && $id_product != $product_download->ID_PRODUCT)) {
		redirect($backlink);
	} else {
		$backlink = Session::getBackLink($section, $backlink);
	}
	$old_file_path = $product_download->getPath();
	$old_is_url = $product_download->IS_URL;
}

//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	$product_download->NAME = trim(Request::get('NAME'));
	$product_download->IS_URL = Request::getNumber('IS_URL');
	$product_download->DISABLED = Request::getNumber('DISABLED');
	if ($product_download->IS_URL) {
		$product_download->FILE = trim(Request::get('URL'));
	}
	
	if ($product_download->validateInput())
	{
		$uploaded_file = $_FILES["FILE"];
		if (!$product_download->IS_URL && $uploaded_file["name"]) {
			if (!$id) {
				$product_download->insert();
			}

			// create dir
			$product_download_dir = Product_Download::getDir();
			if (!file_exists($product_download_dir)) {
				mkdir($product_download_dir, 0777, true);
			}

			$product_download->FILE = str_replace(" ", "_", $uploaded_file["name"]);

			if (move_uploaded_file($uploaded_file["tmp_name"], $product_download->getPath())) {
				if ($id) {
					if (!$old_is_url && $product_download->getPath() != $old_download_path) {
						@unlink($old_download_path);
					}
				}
				$product_download->update();
				redirect($backlink);
			} else {
				if (!$id) {
					$product_download->delete();
				}
			}
		} else {
			if (!$id) {
				$product_download->insert();
			}
			else {
				if (!$old_is_url && $product_download->getPath() != $old_download_path) {
					@unlink($old_download_path);
				}
				$product_download->update();
			}
			redirect($backlink);
		}
	}
}

include_once CKEDITOR_BASE_DIR. "ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = CKEDITOR_BASE_PATH;
$CKEditor->config['height'] = 600;
$CKEditor->config['filebrowserBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html';
$CKEditor->config['filebrowserImageBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Images';
$CKEditor->config['filebrowserFlashBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Flash';
$CKEditor->config['filebrowserUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKEditor->config['filebrowserImageUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Images';
$CKEditor->config['filebrowserFlashUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Flash';
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_PRODUCT_MANAGEMENT => Session::getBackLink("product"),
	_LB_PRODUCT_DETAIL => Session::getBackLink($section),
	($id == 0? _LB_ADD_PRODUCT_DOWNLOAD : _LB_EDIT_PRODUCT_DOWNLOAD) => Utils::getCurrentLink() ));

//if validate is false, display form again
$smarty->assign('cfer', $cfer);
$smarty->assign('product_download', $product_download);
$smarty->assign('backlink', $backlink);
$smarty->assign('CKEditor', $CKEditor);
$smarty->display('product_download_form.html');